const Mock = require('mockjs'); // 假数据
const SqliteDB = require('./sqlite.js').SqliteDB;

module.exports = function (app) {
    const prefix = '/api/system/';
    // 登录相关和获取用户信息
    const USERINFO = Mock.mock({
        'userName': '@name()',
        'sex': 1,
        'id': '@guid()',
        'deptId': '@guid()',
        'roleId': '@guid()',
        'roleName': '超级管理员',
        'userPhoto': 'http://q1.qlogo.cn/g?b=qq&nk=123131897&s=100',
        'email': 'whereispal5@163.com',
        'accountManager': 0,
        'postId': '@guid()',
        'token': '111111'
    });
    // API::登录
    app.post(prefix + 'login', (req, res) => {
        const code = 200;
        const msg = '';
        const data = {
            'res': '登录成功',
            'res_code': 1,
            'token': USERINFO.token
        };
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
    // API::登录
    app.post(prefix + 'staff_login', (req, res) => {
        const code = 200;
        const msg = '';
        const data = {
            'res': '登录成功',
            'res_code': 1
        };
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
    // API::二次验证
    app.post(prefix + 'google_auth', (req, res) => {
        const code = 200;
        const msg = '';
        const data = {
            'res': '二次验证成功',
            'res_code': 1,
            'token': USERINFO.token
        };
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
    // API::新消息
    app.get(prefix + 'getNewMessageNum', (req, res) => {
        const code = 200;
        const msg = '';
        const data = {
            'num': 3,
        };
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
    // API::用户信息
    app.get(prefix + 'getUserInfo', (req, res) => {
        const code = 200;
        const msg = '';
        const data = USERINFO;
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
    // API::注册
    app.get(prefix + 'register', (req, res) => {
        const code = 200;
        const msg = '';
        const data = {
            'res': '',
            'res_code': 1
        };
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
    // API::用户权限
    app.get(prefix + 'powerlist', (req, res) => {
        const code = 200;
        const msg = '';
        const data = {
            'res': '',
            'res_code': 1,
            'list': [
                'sy-xxzx', 'sy-cw', 'sy-error', 'sy-power', 'sy-theme',
                'expv1',
                'expv2-12345', 'expv2-67890', 'expv2-22345', 'expv2-27890',
                'workv2-0', 'workv2-1', 'workv2-2',
                'tool',
                'dsp',
                'oa-renshi', 'oa-renwu', 'oa-gong'
            ]
        };
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
    // API::时间戳
    app.get(prefix + 'time', (req, res) => {
        const code = 200;
        const msg = '';
        const data = '';
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
    // API::登出
    app.get(prefix + 'logout', (req, res) => {
        const code = 200;
        const msg = '';
        const data = {
            'res': '',
            'res_code': 1,
        };
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
    app.get(prefix + 'haha', (req, res) => {
        var file = "xian.db";
        var sqliteDB = new SqliteDB(file);
        /// create table.
        var createTileTableSql = "create table if not exists tiles(level INTEGER, column INTEGER, row INTEGER, content BLOB);";
        var createLabelTableSql = "create table if not exists labels(level INTEGER, longitude REAL, latitude REAL, content BLOB);";
        sqliteDB.createTable(createTileTableSql);
        sqliteDB.createTable(createLabelTableSql);
        const code = 200;
        const msg = '';
        const data = '';
        const time = +(new Date);
        const result = {code, data, msg, time};
        return res.end(JSON.stringify(result));
    });
};
